Arrow keys / Click to navigate

Module 7: CI/CD Pipelines

Advanced Architecting on AWS

CodePipeline โ€ข CodeBuild โ€ข CodeDeploy โ€ข CloudFormation โ€ข CDK

๐ŸŽฏ Module Objectives

๐Ÿ”„ CI/CD Pipeline Architecture

Pipeline metaphor: Source (recipe) โ†’ Build (cook the dish) โ†’ Test (taste it) โ†’ Deploy (serve it). CodePipeline is the kitchen manager ensuring each step happens in order.

๐Ÿ—๏ธ CodeBuild โ€” buildspec.yml

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 18
  pre_build:
    commands:
      - echo Logging in to ECR...
      - aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_URI
  build:
    commands:
      - docker build -t $IMAGE_REPO:$CODEBUILD_RESOLVED_SOURCE_VERSION .
      - docker push $IMAGE_REPO:$CODEBUILD_RESOLVED_SOURCE_VERSION
  post_build:
    commands:
      - printf '[{"name":"app","imageUri":"%s"}]' $IMAGE_REPO:$TAG > imagedefinitions.json
artifacts:
  files: ['imagedefinitions.json']

๐Ÿš€ CodeDeploy Strategies

StrategyBehaviorRisk Level
AllAtOnceDeploy to all targets simultaneouslyHigh (full blast)
Rolling (OneAtATime)Deploy to one instance at a timeMedium
Blue/GreenNew fleet alongside old, switch trafficLow (instant rollback)
Canary (10% then 90%)Small percentage first, then fullLow (early detection)
Linear (10% every 5 min)Gradual shift over timeLowest (slow rollout)
appspec.yml: Defines deployment lifecycle hooks โ€” BeforeInstall, AfterInstall, ApplicationStart, ValidateService. Use hooks for health checks and rollback triggers.

๐Ÿ“‹ CloudFormation Advanced

StackSets use case: Deploy security baseline (Config rules, CloudTrail, GuardDuty) to all accounts in an Organization with a single StackSet.

๐Ÿงฐ AWS CDK

CDK vs. CloudFormation: CDK is to CloudFormation what TypeScript is to raw JSON โ€” same underlying engine, but with programming language power, abstractions, and type safety.

๐Ÿ’ป Demo: CodePipeline + ECS Deploy

# List pipelines
aws codepipeline list-pipelines --query 'pipelines[].name'

# Get pipeline state
aws codepipeline get-pipeline-state --name my-pipeline \
  --query 'stageStates[].{Stage:stageName,Status:latestExecution.status}'

# Start pipeline execution
aws codepipeline start-pipeline-execution --name my-pipeline

# Deploy CloudFormation StackSet
aws cloudformation create-stack-set --stack-set-name security-baseline \
  --template-url https://s3.amazonaws.com/mybucket/security.yaml \
  --permission-model SERVICE_MANAGED --auto-deployment Enabled=true

๐Ÿงช Knowledge Check

Q1: A team needs to deploy the same security configuration across 200 accounts in an Organization. Which CloudFormation feature should they use?

A) Nested Stacks   B) Change Sets   C) StackSets with SERVICE_MANAGED permissions   D) Custom Resources

C) StackSets with SERVICE_MANAGED permissions โ€” StackSets deploy stacks across multiple accounts and Regions. SERVICE_MANAGED permissions integrates with Organizations for automatic deployment to new accounts.

Q2: Which CodeDeploy strategy provides the fastest rollback for ECS deployments?

A) Rolling   B) AllAtOnce   C) Blue/Green   D) In-place

C) Blue/Green โ€” Blue/Green keeps the old task set running alongside the new one. Rollback is instant (re-route traffic back to blue). No need to redeploy the previous version.

๐Ÿ“ Module 7 Summary

CodePipeline

Orchestrates Source โ†’ Build โ†’ Deploy. Cross-account pipelines supported. Integrates with GitHub.

CodeDeploy

Blue/Green for instant rollback. Canary/Linear for gradual. appspec.yml defines lifecycle hooks.

CloudFormation

Nested stacks (modularity), StackSets (multi-account), Change Sets (preview), Drift Detection.

CDK

IaC with real programming languages. L1/L2/L3 constructs. CDK Pipelines for self-mutating CI/CD.

Click anywhere to close